package org.szgb.console.zx.plan.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.sys.department.entity.SysDepartment;
import org.szgb.console.sys.file.entity.SysFile;
import org.szgb.console.sys.system.entity.SysSystem;
import org.szgb.console.sys.user.entity.SysUser;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class ZxPlan extends BeanEntity<ZxPlan> {
    @Serial
    private static final long serialVersionUID = 1L;
    private String name;
    private Integer type;
    private Integer status;
    private SysDepartment responsible;
    private SysUser leader;
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate startAt;
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate endAt;
    private String substationIds;
    private SysSystem system;
    private String equipmentIds;
    private String unit;
    private String requirement;
    private String remark;
    private String planFileIds;
    private String taskFileIds;

    public ZxPlan() {
    }

    public ZxPlan(String id) {
        this.setId(id);
    }


    public ZxPlan(ZxPlan.ZxPlanFindDto zxPlanFindDto) {
        this.setId(zxPlanFindDto.getId());
        this.setName(zxPlanFindDto.getName());
        this.setType(zxPlanFindDto.getType());
        this.setStatus(zxPlanFindDto.getStatus());
        this.setResponsible(new SysDepartment(zxPlanFindDto.getResponsibleId()));
        this.setLeader(new SysUser(zxPlanFindDto.getLeaderId()));
        this.setSubstationIds(zxPlanFindDto.getSubstationIds());
        this.setPageDto(zxPlanFindDto.getPageDto());
    }

    public ZxPlan(ZxPlan.ZxPlanSaveDto zxPlanSaveDto) {
        this.setId(zxPlanSaveDto.getId());
        this.setName(zxPlanSaveDto.getName());
        this.setType(zxPlanSaveDto.getType());
        this.setStatus(zxPlanSaveDto.getStatus());
        this.setResponsible(new SysDepartment(zxPlanSaveDto.getResponsibleId()));
        this.setLeader(new SysUser(zxPlanSaveDto.getLeaderId()));
        this.setSystem(new SysSystem(zxPlanSaveDto.getSystemId()));
        this.setSubstationIds(zxPlanSaveDto.getSubstationIds());
        this.setEquipmentIds(zxPlanSaveDto.getEquipmentIds());
        this.setStartAt(zxPlanSaveDto.getStartAt());
        this.setEndAt(zxPlanSaveDto.getEndAt());
        this.setUnit(zxPlanSaveDto.getUnit());
        this.setRequirement(zxPlanSaveDto.getRequirement());
        this.setRemark(zxPlanSaveDto.getRemark());
        this.setPlanFileIds(zxPlanSaveDto.getPlanFileIds());
        this.setTaskFileIds(zxPlanSaveDto.getTaskFileIds());
    }

    public ZxPlan(ZxPlan.ZxPlanCreateDto zxPlanCreateDto) {
        this.setName(zxPlanCreateDto.getName());
        this.setType(zxPlanCreateDto.getType());
        this.setResponsible(new SysDepartment(zxPlanCreateDto.getResponsibleId()));
        this.setSystem(new SysSystem(zxPlanCreateDto.getSystemId()));
        this.setSubstationIds(zxPlanCreateDto.getSubstationIds());
        this.setEquipmentIds(zxPlanCreateDto.getEquipmentIds());
        this.setStartAt(zxPlanCreateDto.getStartAt());
        this.setEndAt(zxPlanCreateDto.getEndAt());
        this.setUnit(zxPlanCreateDto.getUnit());
        this.setRequirement(zxPlanCreateDto.getRequirement());
        this.setRemark(zxPlanCreateDto.getRemark());
        this.setPlanFileIds(zxPlanCreateDto.getPlanFileIds());
    }

    public ZxPlan(ZxPlan.ZxPlanSetLeaderDto zxPlanSetLeaderDto) {
        this.setId(zxPlanSetLeaderDto.getId());
        this.setLeader(new SysUser(zxPlanSetLeaderDto.getLeaderId()));
        this.setTaskFileIds(zxPlanSetLeaderDto.getTaskFileIds());
    }

    /**
     * 转换为标准视图
     *
     * @param substationNames 变电所名称","隔开
     * @param equipmentNames  设备名称","隔开
     * @return 转换后的视图
     */
    public ZxPlan.ZxPlanView toZxPlanView(String substationNames, String equipmentNames, List<SysFile> planFiles, List<SysFile> taskFiles) {
        return new ZxPlan.ZxPlanView(this, substationNames, equipmentNames, planFiles, taskFiles);
    }

    @Data
    @Schema(title = "专项计划信息", description = "专项计划信息")
    public static class ZxPlanView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "类型")
        private Integer type;
        @Schema(description = "状态")
        private Integer status;
        @Schema(description = "归属部门id")
        private String responsibleId;
        @Schema(description = "归属部门名称")
        private String responsibleName;
        @Schema(description = "牵头人id")
        private String leaderId;
        @Schema(description = "牵头人名称")
        private String leaderName;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "开始时间")
        private LocalDate startAt;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "结束时间")
        private LocalDate endAt;
        @Schema(description = "变电所ids")
        private String substationIds;
        @Schema(description = "变电所名称")
        private String substationNames;
        @Schema(description = "系统id")
        private String systemId;
        @Schema(description = "系统名称")
        private String systemName;
        @Schema(description = "设备ids")
        private String equipmentIds;
        @Schema(description = "设备名称")
        private String equipmentNames;
        @Schema(description = "最小单元")
        private String unit;
        @Schema(description = "数据要求")
        private String requirement;
        @Schema(description = "专项描述")
        private String remark;
        @Schema(description = "专项文件列表")
        private List<SysFile.SysFileView> planFileList;
        @Schema(description = "任务文件列表")
        private List<SysFile.SysFileView> taskFileList;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public ZxPlanView(ZxPlan zxPlan, String substationNames, String equipmentNames, List<SysFile> planFiles, List<SysFile> taskFiles) {
            this.id = zxPlan.getId();
            this.name = zxPlan.getName();
            this.type = zxPlan.getType();
            this.status = zxPlan.getStatus();
            this.responsibleId = zxPlan.getResponsible().getId();
            this.responsibleName = zxPlan.getResponsible().getName();
            if (zxPlan.getLeader() != null) {
                this.leaderId = zxPlan.getLeader().getId();
                this.leaderName = zxPlan.getLeader().getName();
            }
            this.startAt = zxPlan.getStartAt();
            this.endAt = zxPlan.getEndAt();
            this.substationIds = zxPlan.getSubstationIds();
            this.substationNames = substationNames;
            this.systemId = zxPlan.getSystem().getId();
            this.systemName = zxPlan.getSystem().getName();
            this.equipmentIds = zxPlan.getEquipmentIds();
            this.equipmentNames = equipmentNames;
            this.unit = zxPlan.getUnit();
            this.requirement = zxPlan.getRequirement();
            this.remark = zxPlan.getRemark();
            if (zxPlan.getPlanFileIds() != null) {
                this.planFileList = SysFile.SysFileView.getViewListFromList(planFiles);
            }
            if (zxPlan.getTaskFileIds() != null) {
                this.taskFileList = SysFile.SysFileView.getViewListFromList(taskFiles);
            }
            this.createdAt = zxPlan.getCreatedAt();
            this.updatedAt = zxPlan.getUpdatedAt();
            if (zxPlan.getCreatedBy() != null) {
                this.createdById = zxPlan.getCreatedBy().getId();
                this.createdByName = zxPlan.getCreatedBy().getName();
            }
            if (zxPlan.getUpdatedBy() != null) {
                this.updatedById = zxPlan.getUpdatedBy().getId();
                this.updatedByName = zxPlan.getUpdatedBy().getName();
            }
        }
    }

    @Data
    @Schema(title = "专项计划列表信息", description = "专项计划列表信息")
    public static class ZxPlanListView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "类型")
        private Integer type;
        @Schema(description = "状态")
        private Integer status;
        @Schema(description = "归属部门id")
        private String responsibleId;
        @Schema(description = "归属部门名称")
        private String responsibleName;
        @Schema(description = "牵头人id")
        private String leaderId;
        @Schema(description = "牵头人名称")
        private String leaderName;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "开始时间")
        private LocalDate startAt;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "结束时间")
        private LocalDate endAt;
        @Schema(description = "系统id")
        private String systemId;
        @Schema(description = "系统名称")
        private String systemName;
        @Schema(description = "最小单元")
        private String unit;
        @Schema(description = "数据要求")
        private String requirement;
        @Schema(description = "专项描述")
        private String remark;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public ZxPlanListView(ZxPlan zxPlan) {
            this.id = zxPlan.getId();
            this.name = zxPlan.getName();
            this.type = zxPlan.getType();
            this.status = zxPlan.getStatus();
            this.responsibleId = zxPlan.getResponsible().getId();
            this.responsibleName = zxPlan.getResponsible().getName();
            if(zxPlan.getLeader() != null){
            this.leaderId = zxPlan.getLeader().getId();
            this.leaderName = zxPlan.getLeader().getName();
            }
            this.startAt = zxPlan.getStartAt();
            this.endAt = zxPlan.getEndAt();
            this.systemId = zxPlan.getSystem().getId();
            this.systemName = zxPlan.getSystem().getName();
            this.unit = zxPlan.getUnit();
            this.requirement = zxPlan.getRequirement();
            this.remark = zxPlan.getRemark();
            this.createdAt = zxPlan.getCreatedAt();
            this.updatedAt = zxPlan.getUpdatedAt();
            if (zxPlan.getCreatedBy() != null) {
                this.createdById = zxPlan.getCreatedBy().getId();
                this.createdByName = zxPlan.getCreatedBy().getName();
            }
            if (zxPlan.getUpdatedBy() != null) {
                this.updatedById = zxPlan.getUpdatedBy().getId();
                this.updatedByName = zxPlan.getUpdatedBy().getName();
            }
        }

        public static List<ZxPlan.ZxPlanListView> getViewListFromList(List<ZxPlan> zxPlanList) {
            return zxPlanList.stream().map(ZxPlan.ZxPlanListView::new).toList();
        }
    }

    @Data
    @Schema(title = "专项计划查询表单", description = "专项计划查询参数")
    public static class ZxPlanFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "类型")
        private Integer type;
        @Schema(description = "状态")
        private Integer status;
        @Schema(description = "归属部门id")
        private String responsibleId;
        @Schema(description = "牵头人id")
        private String leaderId;
        @Schema(description = "变电所ids")
        private String substationIds;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public ZxPlan toZxPlan() {
            return new ZxPlan(this);
        }
    }

    @Data
    @Schema(title = "专项计划保存表单", description = "专项计划保存参数")
    public static class ZxPlanSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "类型")
        private Integer type;
        @Schema(description = "状态")
        private Integer status;
        @Schema(description = "归属部门id")
        private String responsibleId;
        @Schema(description = "牵头人id")
        private String leaderId;
        @Schema(description = "变电所ids")
        private String substationIds;
        @Schema(description = "系统id")
        private String systemId;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "开始时间")
        private LocalDate startAt;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "结束时间")
        private LocalDate endAt;
        @Schema(description = "设备ids")
        private String equipmentIds;
        @Schema(description = "最小单元")
        private String unit;
        @Schema(description = "数据要求")
        private String requirement;
        @Schema(description = "专项描述")
        private String remark;
        @Schema(description = "专项文件ids")
        private String planFileIds;
        @Schema(description = "任务文件ids")
        private String taskFileIds;

        public ZxPlan toZxPlan() {
            return new ZxPlan(this);
        }
    }

    @Data
    @Schema(title = "专项计划创建表单", description = "专项计划创建参数")
    public static class ZxPlanCreateDto {
        @Schema(description = "名称")
        private String name;
        @Schema(description = "类型")
        private Integer type;
        @Schema(description = "归属部门id")
        private String responsibleId;
        @Schema(description = "变电所ids")
        private String substationIds;
        @Schema(description = "系统id")
        private String systemId;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "开始时间")
        private LocalDate startAt;
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "结束时间")
        private LocalDate endAt;
        @Schema(description = "设备ids")
        private String equipmentIds;
        @Schema(description = "最小单元")
        private String unit;
        @Schema(description = "数据要求")
        private String requirement;
        @Schema(description = "专项描述")
        private String remark;
        @Schema(description = "专项文件ids")
        private String planFileIds;

        public ZxPlan toZxPlan() {
            return new ZxPlan(this);
        }
    }

    @Data
    @Schema(title = "专项计划指定牵头人表单", description = "专项计划指定牵头人参数")
    public static class ZxPlanSetLeaderDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "牵头人id")
        private String leaderId;
        @Schema(description = "任务文件ids")
        private String taskFileIds;

        public ZxPlan toZxPlan() {
            return new ZxPlan(this);
        }
    }
}
